package com.hongbao.game.controller;

import com.hongbao.game.model.BatchPrize;
import com.hongbao.game.model.Game;
import com.hongbao.game.model.User;
import com.hongbao.game.service.GameService_Lua;
import com.hongbao.game.service.PrizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("game")
public class GameController {

    @Autowired
    GameService_Lua gameService;

    @Autowired
    PrizeService prizeService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("attachPrize/{gameId}")
    public String attachPrize(@PathVariable  Long gameId,
            @RequestBody List<Long> prizeIds){

        //获取所有的奖品
        List<BatchPrize> list = prizeService.selectBatchPrizeList(prizeIds);
        //获取活动
        Game game = gameService.findById(gameId);
        //
        gameService.attachPrize(game,list);

        return "OK";
    }

    @Autowired
    HttpServletRequest request;
    @PostMapping("takePartGame/{gameId}")
    public String takePartGame(@PathVariable  Long gameId){
        String token = request.getHeader("token");//zhangsan
        //从redis查找
        User user =(User)redisTemplate.opsForValue().get("token_" + token);

        String prize = gameService.takePartGame(gameId, user);
        return prize;
    }
}
